package com.lsy.weizhi2.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;
import com.lsy.weizhi2.JsonObject.SelfMessageList;
import com.lsy.weizhi2.R;
import com.lsy.weizhi2.activity.MySelfMessage2;
import com.lsy.weizhi2.config.Constants;
import com.lsy.weizhi2.config.MyInfo;
import com.lsy.weizhi2.customview.GlideCircleTransform;
import com.lsy.weizhi2.myutils.ManagerItem;
import com.lsy.weizhi2.utils.MyMethod;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

/**
 * Created by 嗨哟 on 2017/5/6.
 */

public class UnReadAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<SelfMessageList.ListBean> list;
    private LayoutInflater layoutInflater;
    private int bottom_count, tag;

    public UnReadAdapter(Context context, List<SelfMessageList.ListBean> list) {
        this.context = context;
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
    }

    public void UpData(List<SelfMessageList.ListBean> list, int bottom_count, int tag) {
        this.list = list;
        this.tag = tag;
        this.bottom_count = bottom_count;
        this.notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = layoutInflater.inflate(R.layout.read_item, viewGroup, false);
        View view_bottom = layoutInflater.inflate(R.layout.bottom_view, viewGroup, false);
        AutoUtils.autoSize(view);
        AutoUtils.autoSize(view_bottom);
        if (i == ManagerItem.Bottom) {
            return new BottomHolder(view_bottom);
        } else {
            return new ReadItemHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ReadItemHolder) {
            ((ReadItemHolder) holder).position = position;
            if (list != null && list.size() != 0) {
                ((ReadItemHolder) holder).read_item_name.setText(list.get(position).getFriend().getRealName());
                ((ReadItemHolder) holder).read_item_content.setText(list.get(position).getMessageContent());
                ((ReadItemHolder) holder).read_item_time.setText(list.get(position).getCreateTime());

                int status=list.get(position).getStatus();
                if(status==0){
                    ((ReadItemHolder) holder).read_item_num.setVisibility(View.VISIBLE);
                }else{
                    ((ReadItemHolder) holder).read_item_num.setVisibility(View.GONE);
                }
                Glide.with(context).load(new GlideUrl(Constants.DOWN_USER_PICTURE + list.get(position).getFriend().getAvatar(), new LazyHeaders.Builder().addHeader("WZ-AUTH-TOKEN", MyInfo.Token).addHeader("WZ-AUTH-ID", MyInfo.User_Id + "").build()))
                        .transform(new GlideCircleTransform(context))
                        .error(R.mipmap.default_head).into(((ReadItemHolder) holder).read_item_iv);
            }
        } else if (holder instanceof BottomHolder) {
            if (bottom_count == 1 && tag == 0) {
                ((BottomHolder) holder).bottom_tv.setText("松开加载");
            } else if (bottom_count == 1 && tag == 1) {
                ((BottomHolder) holder).bottom_tv.setText("正在加载");
            } else if (bottom_count == 1 && tag == 2) {
                ((BottomHolder) holder).bottom_tv.setText("到底了");
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (list != null && position == list.size()) {
            return ManagerItem.Bottom;
        } else {
            return super.getItemViewType(position);
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 3 + bottom_count : list.size() + bottom_count;
    }

    class ReadItemHolder extends RecyclerView.ViewHolder {
        int position;
        ImageView read_item_iv;
        TextView read_item_name, read_item_num, read_item_content, read_item_time;
        RelativeLayout read_item_view;
        public ReadItemHolder(View itemView) {
            super(itemView);
            read_item_view = (RelativeLayout) itemView.findViewById(R.id.read_item_view);
            read_item_iv = (ImageView) itemView.findViewById(R.id.read_item_iv);
            read_item_name = (TextView) itemView.findViewById(R.id.read_item_name);
            read_item_num = (TextView) itemView.findViewById(R.id.read_item_num);
            read_item_content = (TextView) itemView.findViewById(R.id.read_item_content);
            read_item_time = (TextView) itemView.findViewById(R.id.read_item_time);
            read_item_view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MyMethod.toActivity(context, MySelfMessage2.class,new String[]{"friendId"},new String[]{list.get(position).getFriend().getId()+""});
                }
            });
        }
    }

    class BottomHolder extends RecyclerView.ViewHolder {
        TextView bottom_tv;

        public BottomHolder(View itemView) {
            super(itemView);
            bottom_tv = (TextView) itemView.findViewById(R.id.bottom_tv);
        }
    }
}
